home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / languages / cleo.lzh / Cleo / source / CONVERT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-24  |  3.7 KB  |  132 lines

  1. /***************************************************************************
  2. *   Ce fichier, ainsi que tous les  modules  l'accompagnant, peut et  doit *
  3. * etre  copié GRATUITEMENT à la seule condition expresse de conserver      *
  4. * l'INTEGRALITE  du  Code Source, de  la documentation, et  des fichiers   *
  5. * annexes du package. Ce logiciel est Shareware, veuilez envoyer 100 FF à  *
  6. * l'auteur pour recevoir regulièrement les nouvelles versions.             *
  7. * Toute modification est INTERDITE sans l'autorisation écrite de l'auteur. *
  8. *            Tous droits réservés à M. DIALLO Barrou, Juillet 1992.        *
  9. ***************************************************************************/
  10.  
  11.         /************* Module de conversion ******************/
  12.  
  13. #ifdef msdos
  14.         #include "include\\cleobis.h"
  15. #else
  16.         #include "include/cleobis.h"
  17. #endif
  18.  
  19. /* Fonction qui converti une Chaine d'entier en Long */
  20.  
  21. long Str2Int( char *str)
  22. {
  23.   long n = 0;
  24.   char *c = str;
  25.   while(*c >= '0' && *c <= '9')
  26.     n = n*10 + *c++ - '0';
  27.   return (n);
  28. }
  29.  
  30. /*** Fonction de convertion d'une base quelconque en base 10 */
  31.  
  32. unsigned long Convert2Dec(char *txt,int base, int len)
  33. {
  34.         unsigned long i, digit, res=0;
  35.  
  36.         for (i=0;i< len; i++)
  37.         {
  38.                 digit = txt[len-i-1]-'0';
  39.                 if (digit>9)
  40.                 {
  41.                  if (txt[len-i-1]>='a' && txt[len-i-1] <='f')
  42.                     digit -= ('a'-'9'-1);
  43.                  else
  44.                  if (txt[len-i-1]>='A' && txt[len-i-1] <='F')
  45.                     digit -=('A'-'9'-1);
  46.                 }
  47.                 res += digit*pw(base,i);
  48.         }
  49. return(res);
  50. }
  51.  
  52. /*** Fonction qui calcul la partie d'un nombre après le E */
  53.  
  54. float CalcE( char *txt, int lg)
  55. {
  56.         double mant=0;
  57.         int n=0;
  58.         char neg=0;
  59.         txt++; lg--;
  60.  
  61.         if (*txt =='-')
  62.                 {   txt++; lg--; neg=1; }
  63.         if (*txt =='+')
  64.                 {   txt++; lg--;    }
  65.  
  66.         while (lg)
  67.                 mant += (txt[n++]-'0')*pw(10,--lg);
  68.         if (neg)
  69.                 mant= pw(10,-mant);
  70.         else
  71.                 mant= pw(10,mant);
  72. return((float) mant);
  73. }
  74.  
  75. /*** Fonction qui convertit un nombre entier Ascii en Float */
  76.  
  77. float Ascii2Entier(char *txt, int len)
  78. {
  79.         double res=0, mant=1;
  80.         char *tp, fin=0;
  81.         int n=0, lg, sig=1;
  82.  
  83.         if (*txt=='-')  {sig=-1; txt++; }
  84.         while (*txt=='0') { txt++;len--; }
  85.         tp=txt; lg=len;
  86.         while (lg && !fin)
  87.         {
  88.                 if (*tp == 'E' || *tp == 'e')
  89.                     fin=1;
  90.                 else
  91.                 {   lg--;  tp++;}
  92.         }
  93.         if (fin)
  94.                 {
  95.                     mant = CalcE(tp,lg);
  96.                     len=len-lg;
  97.                 }
  98.                 while(len)
  99.                     res += ((txt[n++])-'0')*pw(10,--len);
  100. return((float) res*mant*sig);
  101. }
  102.  
  103. /*** Fonction qui convertit un nombre reel Ascii en Float */
  104.  
  105. float Ascii2Reel(char *txt, int len)
  106. {
  107.         int lg=0, lg2=0, sig=1;
  108.         float mant=1,res=0,vir=0;
  109.         char *tp, *tp2, fin=0;
  110.  
  111.         if (*txt=='-')  {sig=-1; txt++; }
  112.         while (*txt=='0') { txt++;len--; }
  113.         tp=tp2=txt;
  114.         while (*tp++ !='.') lg++;
  115.         while ( lg2<len && !fin)
  116.                 if (*tp2 =='e' || *tp2=='E') fin=1;
  117.                 else { lg2++; tp2++; }
  118.         if (fin)
  119.                 {
  120.                         mant = CalcE (tp2,len-lg2);
  121.                         res=Ascii2Entier(txt,lg);
  122.                         vir = Ascii2Entier(tp,lg2-2);
  123.                         return((float)(res+vir/pw(10,lg2-2))*mant*sig);
  124.                 }
  125.         else
  126.         {
  127.                 res=Ascii2Entier(txt,lg);
  128.                 vir = Ascii2Entier(tp,len-lg-1);
  129.                 return((float) res+vir/pw(10,len-lg-1)*sig);
  130.         }
  131. }
  132.